var jweixin = require('jweixin-module');
import Request from '@/util/pocky-request/index'
import store from '../store'
import {
	delParam,
	delEmptyParam
} from '../util/stringUtil.js'
export default {
	//判断是否在微信中  
	isWechat: function() {
		var ua = window.navigator.userAgent.toLowerCase();
		if (ua.match(/micromessenger/i) == 'micromessenger') {
			return true;
		} else {
			return false;
		}
	},
	//初始化sdk配置  
	initJssdk: function(callback, url) {
		//服务端进行签名 ，可使用uni.request替换。 签名算法请看文档  
		Request().request({
			url: 'api/CommonTools/JssdkUiPackage',
			data: {
				Url: url
			}
		}).then(res => {
			jweixin.config({
				debug: false,
				appId: res.appId,
				timestamp: res.timestamp,
				nonceStr: res.nonceStr,
				signature: res.signature,
				jsApiList: [
					'checkJsApi',
					'updateAppMessageShareData',
					'updateTimelineShareData',
					'hideAllNonBaseMenuItem',
					'chooseImage',
					'getLocalImgData'

				]
			});
			//配置完成后，再执行分享等功能  
			if (callback) {
				callback(res);
			}
		})
	},
	//在需要自定义分享的页面中调用  
	share: function(data) {
		if (!this.isWechat()) {
			return;
		}
		var url = window.location.href;
		var linkUrl = data && data.link ? data.link : window.location.href;
		if (linkUrl.indexOf('shareUserId') < 0) {
			if (linkUrl.indexOf('?') < 0) {
				linkUrl = linkUrl + '?shareUserId=';
			} else {
				linkUrl = linkUrl + '&shareUserId=';
			}
			linkUrl = linkUrl + store.state.user.shareUserId;
		}

		if (linkUrl.indexOf('token') > 0) {
			linkUrl = delParam(linkUrl, 'token');
		}
		if (linkUrl.indexOf('?&') > 0) {
			linkUrl = linkUrl.replace('?&', '?');
		}
		linkUrl = delEmptyParam(linkUrl);

		//每次都需要重新初始化配置，才可以进行分享  
		this.initJssdk(function(signData) {

			jweixin.ready(function() {
				var shareData = {
					title: data && data.title ? data.title : '云课堂',
					desc: data && data.desc ? data.desc : '这所老年大学为您量身定制，200万新活力老人与您同学',
					link: linkUrl,
					imgUrl: data && data.img ? data.img : 'https://img-ali.cnyanglao.com/logo.jpg-w100',
					success: function(res) {
						//用户点击分享后的回调
						if (data.success && typeof data.success == "function") {
							data.success();
						}
					}
				};
				//分享给朋友接口  
				jweixin.updateAppMessageShareData(shareData);
				//分享到朋友圈接口  
				jweixin.updateTimelineShareData(shareData);
			});
		}, url);
	},
	//禁用分享 复制等菜单按钮功能
	hideMenu: function(callBack) {
		if (!this.isWechat()) {
			return;
		}
		var url = window.location.href;
		this.initJssdk(function(signData) {
			jweixin.ready(function() {
				jweixin.hideAllNonBaseMenuItem();
				if (callBack && typeof callBack == "function") {
					callBack()
				}
			})
		}, url)
	},
	chooseImage: async function(data) {
		if (!this.isWechat()) {
			return;
		}
		if (!data)
			data = {};
		if (!data.count)
			data.count = 9;
		if (!data.sizeType)
			data.sizeType = ['original', 'compressed'];
		if (!data.sourceType)
			data.sourceType = ['album', 'camera'];
		var that = this;
		return new Promise((resolve, reject) => {
			jweixin.chooseImage({
				count: data.count,
				sizeType: data.sizeType,
				sourceType: data.sourceType,
				success: async function(res) {
					var localDatas = await that.handlerLoacalImgData(res.localIds);
					resolve(localDatas)
				}
			})
		})
	},
	handlerLoacalImgData: async function(localIds) {
		var localDatas = new Array();
		for (let index in localIds) {
			var item = localIds[index];
			var localData = await this.getLocalImgData(item);
			localDatas.push(localData);
		}
		return localDatas;
	},
	getLocalImgData: function(item) {
		var isIOS = uni.getSystemInfoSync().platform == 'ios';	
		return new Promise((resolve, reject) => {
			jweixin.getLocalImgData({
				localId: item,
				success: function(res) {
				
					var localData = res.localData;
					if (localData.indexOf("base64,") > -1) {
						localData = localData.substr(localData.indexOf("base64,") + "base64,".length);
					}										
					if (isIOS) {
						resolve({
							localData: localData,
							src: res.localData
						})
					} else {
						resolve({
							localData: localData,
							src: item
						})
					}
				}
			})
		})
	}
}
